package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienDonXinXetMienGiam;
import com.lb.jwitter.service.TblQLDTTXAHocVienDonXinXetMienGiamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiam;

import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiamService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienDonXinXetMienGiamInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public Collection<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private String ngayNop; 
    public void setNgayNop(String ngayNop) {
       this.ngayNop = ngayNop;
 }

    public String getNgayNop() {
       return ngayNop;
 }

    private int tongSoMon; 
    public void setTongSoMon(int tongSoMon) {
       this.tongSoMon = tongSoMon;
 }

    public int getTongSoMon() {
       return tongSoMon;
 }

    private int tronGoi; 
    public void setTronGoi(int tronGoi) {
       this.tronGoi = tronGoi;
 }

    public int getTronGoi() {
       return tronGoi;
 }

    private double tongTienDong; 
    public void setTongTienDong(double tongTienDong) {
       this.tongTienDong = tongTienDong;
 }

    public double getTongTienDong() {
       return tongTienDong;
 }

    private String soBienLai; 
    public void setSoBienLai(String soBienLai) {
       this.soBienLai = soBienLai;
 }

    public String getSoBienLai() {
       return soBienLai;
 }

    private String nguoiThu; 
    public void setNguoiThu(String nguoiThu) {
       this.nguoiThu = nguoiThu;
 }

    public String getNguoiThu() {
       return nguoiThu;
 }

    private TblQLDTTXAHocVienDonXinXetMienGiam tblQLDTTXAHocVienDonXinXetMienGiam; 
    public TblQLDTTXAHocVienDonXinXetMienGiam  getTblQLDTTXAHocVienDonXinXetMienGiam() {
        return tblQLDTTXAHocVienDonXinXetMienGiam;
 }

    private Collection<TblQLDTTXAHocVienDonXinXetMienGiam> tblQLDTTXAHocVienDonXinXetMienGiams; 
    public Collection<TblQLDTTXAHocVienDonXinXetMienGiam>  getTblQLDTTXAHocVienDonXinXetMienGiams() {
         return tblQLDTTXAHocVienDonXinXetMienGiams; 
      } 

    private TblQLDTTXAHocVienDonXinXetMienGiamService tblQLDTTXAHocVienDonXinXetMienGiamService; 

    private String tblQLDTTXAHocVienMienGiamStr; 
    public void setTblQLDTTXAHocVienMienGiamStr(String tblQLDTTXAHocVienMienGiamStr) 
{
       this.tblQLDTTXAHocVienMienGiamStr = tblQLDTTXAHocVienMienGiamStr;
 }

    public String getTblQLDTTXAHocVienMienGiamStr() 
 {
       return tblQLDTTXAHocVienMienGiamStr;
 }

    private TblQLDTTXAHocVienMienGiamService tblQLDTTXAHocVienMienGiamService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

	private int preInsert(Message msg, TblQLDTTXAHocVienDonXinXetMienGiam obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAHocVienDonXinXetMienGiam obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_DonXinXetMienGiam", "them")){ 
        return "noprivilege"; 
    }        tblQLDTTXAHocVienDonXinXetMienGiamService = (TblQLDTTXAHocVienDonXinXetMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_DonXinXetMienGiamClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXAHocVienMienGiamService = (TblQLDTTXAHocVienMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_MienGiamClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        msg = new Message(); 
        tblQLDTTXAHocVienDonXinXetMienGiam = new TblQLDTTXAHocVienDonXinXetMienGiam(); 
        tblQLDTTXAHocVienDonXinXetMienGiam.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayNop); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXAHocVienDonXinXetMienGiam.setNgayNop(d); 
        tblQLDTTXAHocVienDonXinXetMienGiam.setTongSoMon(tongSoMon); 
        if(tronGoi == 1){ 
           tblQLDTTXAHocVienDonXinXetMienGiam.setTronGoi(true); 
       } else { 
           tblQLDTTXAHocVienDonXinXetMienGiam.setTronGoi(false); 
       } 
        tblQLDTTXAHocVienDonXinXetMienGiam.setTongTienDong(tongTienDong); 
        tblQLDTTXAHocVienDonXinXetMienGiam.setSoBienLai(soBienLai); 
        tblQLDTTXAHocVienDonXinXetMienGiam.setNguoiThu(nguoiThu); 
        tblQLDTTXAHocVienDonXinXetMienGiam.setTrangThaiId(1);
        
        preInsert(msg,tblQLDTTXAHocVienDonXinXetMienGiam); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblQLDTTXAHocVienDonXinXetMienGiamService.createtblQLDTTXAHocVienDonXinXetMienGiam(tblQLDTTXAHocVienDonXinXetMienGiam); 
           postInsert(msg, tblQLDTTXAHocVienDonXinXetMienGiam); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblQLDTTXAHocVienDonXinXetMienGiam.setId(id); 
		JSONArray jsonTblQLDTTXAHocVienMienGiam = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienMienGiamStr); 
		for (int i = 0; i < jsonTblQLDTTXAHocVienMienGiam.size(); i++) 
		 { 
		      JSONObject jsObj = jsonTblQLDTTXAHocVienMienGiam.getJSONObject(i); 
		        TblQLDTTXAHocVienMienGiam obj0 = new TblQLDTTXAHocVienMienGiam(); 
		       obj0.setDonMGId(tblQLDTTXAHocVienDonXinXetMienGiam); 
		       obj0.setMonHocId(tblQLDTTXAMonHocService.gettblQLDTTXAMonHoc(new Long(jsObj.getInt("monHocId")))); 
		       obj0.setDuocMienGiam(false); 
		       obj0.setGhiChu(""); 
		       tblQLDTTXAHocVienMienGiamService.createtblQLDTTXAHocVienMienGiam(obj0); 
		   } 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_HocVien_DonXinXetMienGiam Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
